<?php

namespace app\merchants\controller;

use app\merchants\loader\Services;
use app\merchants\validate\RechargeValid;
use think\App;
use think\Exception;

/**
 * 商户充值
 */
class Recharge extends ApiBase
{

    /**
     * 充值
     * @return void
     */
    public function balance(): void
    {
        //recharge_log
        /*   {
               "pay_way": 1,
               "pay_money": -2799067183380996,
               "memo": "quis in nisi sed"
           }*/
        try {
            $postData = input('post.');
            validate(RechargeValid::class)->scene('balance')->check($postData);
            if (empty($this->merId)) {
                throw new Exception('异常系统,未获取商户ID', API_ERROR);
            }
            $result = Services::Recharge()->balance(
                $this->merId,
                $postData['pay_way'],
                $postData['pay_money'],
                $this->adminUser['admin_id'],
                $postData['memo'] ?? ''
            );
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 支付订单详情
     * @return void
     */
    public function pay_info(): void
    {
        try {
            $getData = input('get.');

            if (empty($getData['pay_order'])) {
                api_result(API_ERROR, "支付单号不能为空");
            }
            $result = Services::Recharge()->payInfo(
                $this->merId,
                $getData['pay_order'],
            );
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 格式化where条件
     * @param $getData
     * @return array
     */
    protected function formatWhere($getData): array
    {
        $where[] = ['mer_id', '=', $this->merId];

        if (isset($getData['pay_status']) && ($payStatus = trim($getData['pay_status'])) !== '') {
            $where[] = ['a.pay_status', '=', $payStatus];
        }
        if (!empty($getData['pay_type']) && ($payType = trim($getData['pay_type'])) !== '') {
            $where[] = ['a.pay_type', '=', $payType];
        }
        if (!empty($payOrder = trim($getData['pay_order'] ?? ''))) {
            if ($payOrder[0] !== 'm') {
                $where[] = ['a.out_pay_order', '=', $payOrder];
            } else {
                $where[] = ['a.pay_order', '=', $payOrder];
            }
        }
        $stime = trim($getData['stime'] ?? '');
        $etime = trim($getData['etime'] ?? '');
        if (!empty($stime) && !empty($etime)) {
            $where[] = ['a.create_at', 'between', [$stime . ' 00:00:00', $etime . ' 23:59:59']];
        } else {
            if (!empty($stime)) {
                $where[] = ['a.create_at', '>=', $stime];
            }
            if (!empty($etime)) {
                $where[] = ['a.create_at', '<=', $etime];
            }
        }
        return $where;
    }

    /**
     * 充值流水
     * @return void
     */
    public function balance_page(): void
    {
        try {
            $getData = input('get.');
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $list = Services::Recharge()->balanceLog($option);
            api_result(API_SUCCESS, '请求成功', $list);
        } catch (Exception $e) {
            api_result($e);
        }

    }


    protected function formatOptionMoney(array $getData = []): array
    {
        $where[] = ['mer_id', '=', $this->merId];


        if (!empty($getData['type']) && ($type = trim($getData['type'])) !== '' && $type !== '0' && $type !== 'all') {
            $where[] = ['type', '=', $type];
        }
        $stime = trim($getData['start_time'] ?? '');
        $etime = trim($getData['end_time'] ?? '');
        if (!empty($stime) && !empty($etime)) {
            $where[] = ['create_at', 'between', [$stime . ' 00:00:00', $etime . ' 23:59:59']];
        } else {
            if (!empty($stime)) {
                $where[] = ['create_at', '>=', $stime];
            }
            if (!empty($etime)) {
                $where[] = ['create_at', '<=', $etime];
            }
        }
        return $where;
    }

    /**
     * 充值
     * @return void
     */
    public function money_log_page(): void
    {
        try {
            $getData = input('get.');
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatOptionMoney($getData);
            $list = Services::Recharge()->moneyLogPage($option);
            api_result(API_SUCCESS, '请求成功', $list);
        } catch (Exception $e) {
            api_result($e);
        }

    }


    private function formatWhereVirtual($getData)
    {
        $where = [
            ['mer_id', '=', $this->merId],
            ['flag', '=', 1],
        ];

        if (isset($getData['type']) && ($type = trim($getData['type'])) !== '') {
            $where[] = ['type', '=', $type];
        }
        $stime = trim($getData['stime'] ?? '');
        $etime = trim($getData['etime'] ?? '');
        if (!empty($stime) && !empty($etime)) {
            $where[] = ['create_at', 'between', [$stime . ' 00:00:00', $etime . ' 23:59:59']];
        } else {
            if (!empty($stime)) {
                $where[] = ['create_at', '>=', $stime];
            }
            if (!empty($etime)) {
                $where[] = ['create_at', '<=', $etime];
            }
        }
        return $where;
    }

    /**
     * 虚码交易
     * @return void
     */
    public function virtual_transaction(): void
    {
        //virtual_transaction
        try {
            $getData = input('get.');
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhereVirtual($getData);
            $list = Services::Recharge()->virtualLog($option);
            api_result(API_SUCCESS, '请求成功', $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 购买虚码
     * @return void
     */
    public function buy_virtual(): void
    {

        try {
            $postData = input('post.');
            validate(RechargeValid::class)->scene('buy_virtual')->check($postData);
            $result = Services::Recharge()->buyVirtual(
                $this->merId,
                $this->adminUser['admin_id'],
                $postData['package_id'],
                $postData['memo'] ?? ''
            );
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
        // virtual_transaction
    }


}
